package edu.kit.aifb.ldstream.learner.tasks;

import org.openrdf.model.Statement;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;

import edu.kit.aifb.ldstream.env.Environment;
import edu.kit.aifb.ldstream.learner.dao.SimpleStatistics;

public class NumTriplesTask extends LearnerTask {


	private SimpleStatistics stats;


	public NumTriplesTask(SimpleStatistics stats){
		this.stats  = stats;
	}


	@Override
	public void run() {
		startup();
		if(Environment.VERBOSE){
			System.out.println("Started to cout the number of triples.");
		}
		numTriples();
		shutdown();
	}


	private void numTriples(){
		try {
			int size = 0;
			RepositoryResult<Statement> result = this.con.getStatements(null, null, null, false, Environment.STREAM_CONTEXT_URI);
			try{
				while(result.hasNext()){
					result.next();
					size++;
				}
			}finally{
				result.close();
			}
			if(Environment.VERBOSE){
				System.out.println("NumTriples: " + size);
			}
			this.stats.setNumTriples(size);
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}


}
